package entity

import (
	"fmt"
	"strings"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

// ShJhcwJxffYwjx
type ShJhcwJxffYwjx struct {
	ID    int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	FFJD  time.Time `gorm:"column:FFJD"`
	DPID  int       `gorm:"column:DPID"`
	DPNM  string    `gorm:"column:DPNM"`
	EMID  int       `gorm:"column:EMID"`
	EMNO  string    `gorm:"column:EMNO"`
	EMNM  string    `gorm:"column:EMNM"`
	JXXTZ float64   `gorm:"column:JXXTZ"`
	DRID  int       `gorm:"column:DRID"`
}

// TableName
func (ShJhcwJxffYwjx) TableName() string {
	return "SH_JHCW_JXFF_YWJX"
}

func (u *ShJhcwJxffYwjx) BeforeSave(tx *gorm.DB) (err error) {
	var emid, dpid, pdid *int
	var dpnm *string
	var emno, emnm string
	jdint := datenfjj(u.FFJD)
	err = can_import(tx, jdint)
	if err != nil {
		return
	}
	tx.Raw(`select "EMID","EMNM" from "SH_JHCW_JXFF_YGJX" where "FFJD"=? AND "EMNO" = ?`, jdint, u.EMNO).Row().Scan(&emid, &emnm)
	if emid == nil {
		err = tx.Raw(`select "DPID","DPNM","EMID","EMNO","EMNM","PDID" from "V_USER_DEPT" where "EMNO"=?`, u.EMNO).Row().Scan(&dpid, &dpnm, &emid, &emno, &emnm, &pdid)
		if err != nil || emid == nil {
			err = fmt.Errorf("员工号%s在类别绩效或绩效系统中不存在", u.EMNO)
			return
		}
		err = tx.Exec(`insert into "SH_JHCW_JXFF_YGJX" ("FFJD","DPID","DPNM","EMID","EMNO","EMNM","PDID") values(?,?,?,?,?,?,?)`, jdint, dpid, dpnm, emid, emno, emnm, pdid).Error
		if err != nil {
			return
		}
	}
	emnm_sys := strings.TrimSpace(emnm)
	emnm_imp := strings.TrimSpace(u.EMNM)
	if !strings.EqualFold(emnm_sys, emnm_imp) && !strings.Contains(emnm_sys, emnm_imp) {
		err = fmt.Errorf("员工号%s对应的姓名是%s,而提供姓名是%s", u.EMNO, emnm, u.EMNM)
		return
	}
	tx.Raw(`select "DPNM","DPID"  from "SH_JHCW_JXFF_YGJX" where "EMNO" = ? AND  "FFJD"=? `, u.EMNO, jdint).Row().Scan(&dpnm, &dpid)
	if dpid == nil {
		err = fmt.Errorf("部门%s在类别绩效中不存在,或者类别绩效没有导入", u.DPNM)
		return
	}
	u.DPID = *dpid
	u.DPNM = *dpnm
	u.EMID = *emid
	return
}

func ShJhcwJxffYwjxAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	ffjd := params["FFJD"]
	var date time.Time
	date, err = time.Parse("2006-01-02", ffjd)
	if err != nil {
		return
	}
	quarter := datenfjj(date)
	var sqlstr string
	sqlstr = `UPDATE "SH_JHCW_JXFF_YGJX" SET "YWJX" = 0 WHERE "FFJD" = ?`
	err = tx.Exec(sqlstr, quarter).Error
	if err != nil {
		return
	}
	if base.AppConf.Database.Mysql != "" {
		sqlstr = `UPDATE "SH_JHCW_JXFF_YGJX" A,(select "EMNO","EMID",sum("JXXTZ") "JXXTZ" from "SH_JHCW_JXFF_YWJX" where "FFJD"=? group by "EMNO","EMID") B
		set A."EMNO" = B."EMNO",A."EMID" = B."EMID",A."YWJX" = B."JXXTZ"
		WHERE A."FFJD" = ? AND A."EMNO" = B."EMNO"`
	} else {
		sqlstr = `UPDATE "SH_JHCW_JXFF_YGJX" A
		set "EMNO" = B."EMNO","EMID" = B."EMID","YWJX" = B."JXXTZ"
		FROM (select "EMNO","EMID",sum("JXXTZ") "JXXTZ" from "SH_JHCW_JXFF_YWJX" where "FFJD"=? group by "EMNO","EMID") B
		WHERE A."FFJD" = ? AND A."EMNO" = B."EMNO"`
	}
	err = tx.Exec(sqlstr, ffjd, quarter).Error
	if err != nil {
		return
	}
	err = ImpJxffAfter(tx, quarter)
	return
}

func ShJhcwJxffYwjxEF() base.EntityFunc {
	return func() interface{} {
		entity := ShJhcwJxffYwjx{}
		return &entity
	}
}
func ShJhcwJxffYwjxESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ShJhcwJxffYwjx
		return &entities
	}
}

func init() {
	base.SetAfterCallBack("ShJhcwJxffYwjx", ShJhcwJxffYwjxAfterImport)
	base.DaoPoolObj.Put("ShJhcwJxffYwjx", base.NewDefaultDao(ShJhcwJxffYwjxEF(), ShJhcwJxffYwjxESF(), nil, "my"))
}
